HDOJ 1215 【简单题】七夕节 & HDOJ 1406 【简单题】完数

【题目描述】
P1215:
http://acm.hdu.edu.cn/showproblem.php?pid=1215
P1406:
http://acm.hdu.edu.cn/showproblem.php?pid=1406

【思路】
这2个题差不多的,找完数。纯枚举..
P.S.1406有坑,sum1可能会大于sum2,要提前判断一下。

1215:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<stdio.h>
#include<math.h>
int main()
{
int T,n,x;
long long sum;
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
sum=0;
x=floor(sqrt(n)+0.5);
for (int i=1;i<=x;i++)
if (n%i==0)
{
sum+=i;
if (n/i<n && n/i>i) sum+=n/i;
}
printf("%I64d\n",sum);
}
return 0;
}

1406:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int f[11000];
int main()
{
int T,a,b,n,x;
long long sum;
f[0]=0;f[1]=0;
for (int n=2;n<11000;n++)
{
sum=0;
x=floor(sqrt(n)+0.5);
for (int i=1;i<=x;i++)
if (n%i==0)
{
sum+=i;
if (n/i<n && n/i>i) sum+=n/i;
}
if (sum==n) f[n]=f[n-1]+1;
else f[n]=f[n-1];
}
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&a,&b);
if (a>b) swap(a,b);
printf("%d\n",f[b]-f[a-1]);
}
return 0;
}
文章目录
|